package trunk.component;

import trunk.res.R;

import javax.swing.*;
import java.awt.*;

/**
 * ALU for a algorithm and logic unit
 * Created by IntelliJ IDEA.
 * User: yipeipei
 * Date: 11/28/11
 * Time: 10:53 PM
 * To change this template use File | Settings | File Templates.
 */
public class ALU extends JComponent implements StatusAlterable {
    public static int WIDTH = R.attribute.REGISTER_WIDTH;
    public static int HEIGHT = R.attribute.REGISTER_HEIGHT + 10;

    private int x_UP;
    private int y_UP;
    private int width;
    private int height;
    private Status status = Status.NORMAL;

    private String name = "ALU";

    private int[][] auxiliary = new int[7][2];
    private int[][] joinPoint = new int[6][2];

    private int[] xpoint = new int[8];
    private int[] ypoint = new int[8];

    public ALU(int x_UP, int y_UP) {
        this.x_UP = x_UP;
        this.y_UP = y_UP;
        this.width = WIDTH;
        this.height = HEIGHT;

        initializeAuxiliary();


    }

    public void initializeAuxiliary() {
        //up-left point
        this.auxiliary[0][0] = x_UP - width / 2;
        this.auxiliary[0][1] = y_UP;

        this.auxiliary[1][0] = x_UP + width / 2;
        this.auxiliary[1][1] = y_UP;

        this.auxiliary[2][0] = x_UP + width;
        this.auxiliary[2][1] = y_UP + height;

        this.auxiliary[3][0] = x_UP + 5;
        this.auxiliary[3][1] = y_UP + height;

        this.auxiliary[4][0] = x_UP;
        this.auxiliary[4][1] = y_UP + height - 5;

        this.auxiliary[5][0] = x_UP - 5;
        this.auxiliary[5][1] = y_UP + height;

        this.auxiliary[6][0] = x_UP - width;
        this.auxiliary[6][1] = y_UP + height;

        for (int i = 0; i < 7; i++) {
            xpoint[i] = auxiliary[i][0];
        }
        xpoint[7] = auxiliary[0][0];
        for (int i = 0; i < 7; i++) {
            ypoint[i] = auxiliary[i][1];
        }
        ypoint[7] = auxiliary[0][1];
    }

    public void initializeJointPoint() {

    }

    @Override
    public void paint(Graphics g) {
        //   g.drawLine(auxiliary[0][0],auxiliary[0][1],auxiliary[1][0],auxiliary[1][1]);
        //   g.drawLine(auxiliary[1][0],auxiliary[1][1],auxiliary[2][0],auxiliary[2][1]);
        //   g.drawLine(auxiliary[2][0],auxiliary[2][1],auxiliary[3][0],auxiliary[3][1]);
        //   g.drawLine(auxiliary[3][0],auxiliary[3][1],auxiliary[4][0],auxiliary[4][1]);
        //   g.drawLine(auxiliary[4][0],auxiliary[4][1],auxiliary[5][0],auxiliary[5][1]);
        //   g.drawLine(auxiliary[5][0],auxiliary[5][1],auxiliary[6][0],auxiliary[6][1]);
        //   g.drawLine(auxiliary[6][0],auxiliary[6][1],auxiliary[0][0],auxiliary[0][1]);
        if (status == Status.NORMAL) {
            g.setColor(Color.black);
        } else if (status == Status.ACTIVE) {
            g.setColor(Color.red);
        }

        g.fillPolygon(xpoint, ypoint, 8);

        {
            g.setColor(Color.white);
            g.drawString(this.name, this.x_UP - 10, y_UP + 20);
            g.setColor(Color.black);
        }

        g.setColor(Color.black);
    }

    public int getX_UP() {
        return x_UP;
    }

    public int getY_UP() {
        return y_UP;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public void setStatus(Status status) {
        this.status = status;
    }
}
